package com.lujieni.springbootflowable;

import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 候选人组
 */
@SpringBootTest
class SpringbootFlowableApplicationTests4 {

    @Autowired
    private IdentityService identityService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RuntimeService runtimeService;


    /**
     *
     * 维护用户
     *
     * act_id_user中存入
     *
     */
    @Test
    void createUser() {
        User lujieni = identityService.newUser("lujieni");
        lujieni.setEmail("982584989@qq.com");
        lujieni.setPassword("123456");
        lujieni.setFirstName("lu");
        lujieni.setLastName("jieni");
        identityService.saveUser(lujieni);
    }

    /**
     * 创建用户组
     * 
     * act_id_group中存入
     */
    @Test
    void createGroup() {
        Group salesGroup = identityService.newGroup("xsb");
        salesGroup.setName("销售部");
        salesGroup.setType("department"); // 设置组类型为部门
        identityService.saveGroup(salesGroup);
        System.out.println("用户组创建成功: ID=" + salesGroup.getId() + ", 名称=" + salesGroup.getName());
    }

    /**
     * 绑定用户和用户组的关联关系
     * 
     * act_id_membership中存入
     */
    @Test
    void bindUserToGroup() {
        String userId = "lujieni";
        String groupId = "xsb";
        
        // 将用户lujieni添加到销售部(xsb)组中
        identityService.createMembership(userId, groupId);
        System.out.println("用户关联成功: 用户ID=" + userId + ", 用户组ID=" + groupId);
    }

    @Test
    void deploy() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("process/holiday.bpmn20.xml")
                .name("候选人组")
                .deploy();
        System.out.println(deployment.getId());
    }

    @Test
    void startProcess() {
        runtimeService.startProcessInstanceById("holiday:1:fcc5a665-8745-11f0-b119-5847ca7eb09c");
    }

    /**
     * 根据当前用户所属的组查询组下面的代办任务并拾取
     */
    @Test
    void findTasksByUserGroup() {
        String userId = "lujieni";
        
        // 1. 查询用户所属的组（只有一个）
        Group userGroup = identityService.createGroupQuery()
                .groupMember(userId)
                .singleResult();
        
        if (userGroup != null) {
            System.out.println("用户 " + userId + " 所属的组: " + userGroup.getId() + " - " + userGroup.getName());
            
            // 2. 查询该组的候选任务
            List<Task> groupTasks = taskService.createTaskQuery()
                    .taskCandidateGroup(userGroup.getId())
                    .list();
            
            System.out.println("该组的代办任务数量: " + groupTasks.size());
            for (Task task : groupTasks) {
                System.out.println("- 任务ID: " + task.getId() + 
                                 ", 任务名称: " + task.getName() + 
                                 ", 审批人: " + (task.getAssignee() != null ? task.getAssignee() : "未分配") +
                                 ", 流程实例ID: " + task.getProcessInstanceId());
                taskService.claim(task.getId(), "lujieni");
            }
        } else {
            System.out.println("用户 " + userId + " 没有加入任何组");
        }
    }




}
